import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'

// import App from './views/login/index.vue'
import App from './App'
// import store from './vuex/store'

//ajax-config
import store from './store.js'
import ajaxManager from 'ajax-manager'
import {apiHost} from '../app-config'

//api models
import apiModel from './models/api.model.js'

//ui components
import { Loading, Message } from 'element-ui'

Vue.use(Router);

// 全局组件
const globalComponents = [
  require('./components/layout/top-bar.vue')
];

globalComponents.map(m => {
  Vue.component(m.name, m);
});

Vue.config.productionTip = false;

let router = new Router({ routes });
// 导航跳转等待动画
let routerLoadingInstance = null;
/* router.beforeEach((to, from, next) => {
  routerLoadingInstance = Loading.service();
  next()
});
router.afterEach(route => {
  if(routerLoadingInstance){
	routerLoadingInstance.close();
  }
  //$('.el-loading-mask').remove()
}); */

Object.defineProperty(
  Vue.prototype,
  'api',
  {
    value: ajaxManager([
      apiModel
    ], {
      // any global config
      /* eslint-disable */
      __apiRoot: '/api',//透传方式
      // __apiRoot: 'http://10.200.10.22:28080',//直连方式
      method: 'post',
      // data: {
      //   ver: '0.1'
      // },
      contentType: 'application/json',
      error (xhr, textStatus, err) {
        Message({
          message: '请求失败，请稍后重试',
          type: 'error'
        })
      },
      success (data) {
        if (
          (400 <= data.statusCode && 500 > data.statusCode) ||
          (500 <= data.statusCode && 600 > data.statusCode)
        ) {
          Message({
            message: '请求失败，请稍后重试',
            type: 'error'
          })
        }
      }
      /* eslint-enable */
    }, $)
  }
);

const eventBus = new Vue(store)
const app = new Vue({
  // store,
  router,
  template: '<App/>',
  components: {App},
  data: { eventBus }
}).$mount('#app');
